/**
 * Created by DevilsEyes on 2016/3/14.
 */
'use strict';

define(['text!path/orderDetail/tmp.html', 'address', 'timer', 'payment', 'layer'], function (page, address, timer, payment, layer) {

    var render = template.compile(page);

    var apiList = {
        getInfo: api + 'user/order_detail'
    };

    var isImportantStatus = function (status, cancel) {
        if (status == '0' && !cancel)
            return true;//"待支付";
        if (('10' == status || status == '20') && !cancel)
            return true;//"待发货";

        if ((status == '30') && !cancel)
            return true;//"已发货";

        return false;
    };

    var getStatusTitle = function (status, cancel) {
        if (status == '0') {
            if (cancel > 0) {
                if (cancel == 4)
                    return "支付超时";
                return "已取消";
            }
            return "待支付";
        }
        if ('10' == status) {
            if (cancel > 0) {
                return "已取消";
            }
            return "待发货";
        }

        if ('20' == status) {

            return "待发货";
        }

        if (status == '30')
            return "已发货";

        if (status == '40')
            return "已收货";
        if (status == '100')
            return "已完成";

        return "未知状态,请联系客服";
    };

    var vm = new Page.creat({
        $id: 'orderDetail',
        $model: {
            data: {},
            $: {},
            order_id: null,
            bill_id: null,
            init: function (id) {
                vm.$init({
                    setTitle: '订单详情'
                });

                vm.order_id = id;

                ex.jsonp({
                    url: apiList.getInfo,
                    data: {
                        _id: id,
                    },

                    success: function (obj) {
                        console.log(obj);

                        vm.data = obj.data;
                        vm.data.Pares_address = address.getAddress;

                        //vm.data.canPayTime = 10000;
                        //vm.data.cancel = null;
                        
                        vm.data.isImportantStatus = isImportantStatus;
                        vm.data.getStatusTitle = getStatusTitle;
                        vm.data.discount = obj.data.amount + obj.data.postage.cost - obj.data.needPay;

                        $('#page').html(render(vm.data));

                        vm.bill_id = vm.data.bill_id;

                        if (vm.data.status == 0 && !vm.data.cancel)
                            ex.async(function () {

                                vm.bindEvent(vm.event);
                                vm.bind$(vm.$);

                                $('.tag-timer').each(function () {
                                    var $this = $(this);
                                    var start = $this.attr('data-time');

                                    var id = timer.register($this, function (time) {
                                        var t = start - (time - this.creatTime);
                                        if (t < 0) {
                                            timer.logout(id);
                                            this.$dom.find('span').html('支付超时');
                                            vm.$.header.removeClass('yellow');
                                            vm.$.header.addClass('ui-box');
                                            vm.$.btnPay.hide();
                                        } else {
                                            this.$dom.find('span').html(template.compile("{{t | formatTime:'等待支付 - 剩余mm分ss秒'}}")({t: t}))
                                        }
                                    })
                                })
                            });

                    },
                    error: function (obj) {
                        if (obj.msg) {
                            layer.msg(obj.msg);
                        }
                    }
                })

            },
            event: {
                clickfb: function () {
                    payment.start({
                        close: true,
                        bill_id: vm.bill_id,
                        order_id: vm.order_id,
                    });
                }
            }
        }
    });

    return {
        init: function (params) {
            vm.$init({
                setTitle: 'orderDetail'
            });
            vm.init(params.id);
        }
    }
});